< previous page page_146 next page >

Page 146
Constants
A constant is a value that does not change throughout the life of your program. Constants allow you to assign unmeaningful, magic numbers to a mnemonic name that will be much easier to remember. The reason you would create a constant is to avoid hard-coding a number in several locations throughout your program. If you were to hard-code a number in several places, what would happen if that number were to change? You would have to remember to go to each of those places and change the number manually. By creating a constant, you can assign the number to a meaningful name in one place in your program and then use that meaningful name everywhere else in your program. Now if you need to change this value, you just change it in the one place, and everywhere its used in your program will automatically use this new value.
Constants are like variables in that they have scope and a lifetime. They are different from variables in that they may not be changed during the program. Where you declare a constant will determine its scope and lifetime, just like a variable. Lets look at an example of using constants. You can refer to the example form shown in Figure 7.4.
8160-0146a.gif
Figure 7.4
Example of constants.
Examples Filename: Constants.frm
Local Constants
The first type of constant well look at is a local constant declared within an event procedure. This type of constant is not used very often, but it can come in handy in some situations.

 
< previous page page_146 next page >

If you like this book, buy it!